package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.entity.Placard;
import com.sixmac.service.OperaService;
import com.sixmac.service.PlacardService;
import com.sixmac.utils.DateUtils;
import com.sixmac.utils.JsonUtil;
import com.sixmac.utils.MessageTimerWork;
import com.sixmac.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/30 0030.
 */
@Controller
@RequestMapping(value = "backend/placard")
public class PlacardController extends CommonController {

    @Autowired
    private PlacardService placardService;

    @Autowired
    private OperaService operaService;

    @RequestMapping("/index")
    public String index() {
        return "backend/公告列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String title,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);

        Page<Placard> page = placardService.pageByParams(title, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/show")
    public String info(ModelMap model, Integer id) {
        if (null != id) {
            Placard placard = placardService.getById(id);
            model.addAttribute("placard", placard);
        }

        return "backend/编辑公告";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(HttpServletRequest request, Integer id, String title, String description, String sendTime) {
        try {
            Placard placard = null;

            if (!DateUtils.dateCompare(DateUtils.stringToDateWithFormat(sendTime, "yyyy-MM-dd hh:mm:ss"))) {
                return -1;
            }

            if (null == id) {
                placard = new Placard();
                placard.setCreateTime(new Date());
                placard.setStatus(Constant.MESSAGE_STATUS_000);
            } else {
                placard = placardService.getById(id);
                // 当公告的状态为已撤回时，将公告状态更改为待发送
                if (placard.getStatus() == Constant.MESSAGE_STATUS_002) {
                    placard.setStatus(Constant.MESSAGE_STATUS_000);
                    placard.setSendTime(null);
                    placard.setBackTime(null);
                }
            }

            placard.setTitle(title);
            placard.setDescription(description);
            if (null != sendTime) {
                placard.setSendTime(DateUtils.stringToDateWithFormat(sendTime, "yyyy-MM-dd hh:mm:ss"));
            }

            placardService.update(placard);

            if (placard.getStatus() == Constant.MESSAGE_STATUS_000) {
                new MessageTimerWork(placard.getSendTime(), null, placard, 2);
            }

            operaService.addOperaInfo(request, (null == id ? "添加" : "修改") + "公告 " + placard.getTitle());

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Integer update(HttpServletRequest request, Integer id, Integer status) {
        try {
            Placard placard = placardService.getById(id);
            if (status == Constant.MESSAGE_STATUS_001) {
                placard.setSendTime(new Date());
            } else {
                placard.setBackTime(new Date());
            }
            placard.setStatus(status);
            placardService.update(placard);

            operaService.addOperaInfo(request, "将公告 " + placard.getTitle() + " 状态更改为 " + (placard.getStatus() == Constant.MESSAGE_STATUS_001 ? "已发送" : "已撤回"));

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(HttpServletRequest request, Integer id) {
        try {
            placardService.deleteById(request, id);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除公告
     *
     * @param ids
     * @return
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Integer batchDel(HttpServletRequest request, String ids) {
        try {
            int[] arrayId = JsonUtil.json2Obj(ids, int[].class);
            placardService.deleteAll(request, arrayId);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
